package com.ruoyi.yljf.controller.AppController;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yljf.domain.UserQuestionRecord;
import com.ruoyi.yljf.domain.dto.PracticeDTO;
import com.ruoyi.yljf.domain.vo.QuestionVO;
import com.ruoyi.yljf.service.IExamSummaryService;
import com.ruoyi.yljf.service.IQuestionService;
import com.ruoyi.yljf.service.IUserQuestionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 课程号考试
 */
@RestController
@RequestMapping("/api/paper")
public class ApiPaperController {

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IUserQuestionRecordService userQuestionRecordService;

    @Autowired
    private IExamSummaryService examSummaryService;


    @GetMapping("paperQuestionlist/{taxonId}")
    public AjaxResult getPaperQuestion(@PathVariable("taxonId")Long taxonId) {
        List<QuestionVO> paperQuestionList = questionService.getPaperQuestionList(taxonId);
        return AjaxResult.success(paperQuestionList);
    }

    /**
     * 提交课程考试单题答题记录
     * @param practiceDTO
     * @return
     */
    @PostMapping("/submitQuestion")
    public AjaxResult submitQuestion(@RequestBody PracticeDTO practiceDTO) {
        UserQuestionRecord userQuestionRecord = BeanUtil.copyProperties(practiceDTO, UserQuestionRecord.class);
        userQuestionRecord.setUserId(SecurityUtils.getUserId());
        userQuestionRecord.setCreateTime(new Date());
        // 更新用户章节练习记录
        userQuestionRecordService.submitPaperQuestion(userQuestionRecord);
        return AjaxResult.success();
    }

    /**
     * 提交课程考试试卷，生成考试记录
     * @param taxonId
     * @return
     */
    @GetMapping("/submitPaper")
    public AjaxResult submitPaper(Long taxonId, @DateTimeFormat(pattern = "HH:mm:ss") Date totalTime) {
        examSummaryService.submitPaper(taxonId, totalTime);
        return AjaxResult.success();
    }
}
